
import subprocess

global MeshScript
global CurrentNodeNo
global CurrentEleNo

MeshScript = '/media/chenting/Work/ProgramCode/OpenSeesModellingHelper/MeshIntoShellElements9Nodes.py'
CombineScript = '/media/chenting/Work/ProgramCode/OpenSeesModellingHelper/CombineSameNodes9Nodes.py'
CurrentNodeNo = 1
CurrentEleNo = 1

def MeshQuad(FilePath, ResultPath, ResultName, XCoord, YCoord, ZCoord, StartN, StartE, Div1, Div2, EleType, SectionNo, wait = True):
    global MeshScript
    global CurrentNodeNo
    global CurrentEleNo

    if EleType == 'ShellNL':
        CurrentNodeNo = CurrentEleNo + (2*Div1 + 1)*(2*Div2 + 1)
        CurrentEleNo = CurrentEleNo + Div1*Div2

    newf = open(FilePath, 'w')
    MeshS = open(MeshScript, 'r')

    newf.write('xa = %f' % XCoord[0])
    newf.write('\n')
    newf.write('ya = %f' % YCoord[0])
    newf.write('\n')
    newf.write('za = %f' % ZCoord[0])
    newf.write('\n')

    newf.write('xb = %f' % XCoord[1])
    newf.write('\n')
    newf.write('yb = %f' % YCoord[1])
    newf.write('\n')
    newf.write('zb = %f' % ZCoord[1])
    newf.write('\n')

    newf.write('xc = %f' % XCoord[2])
    newf.write('\n')
    newf.write('yc = %f' % YCoord[2])
    newf.write('\n')
    newf.write('zc = %f' % ZCoord[2])
    newf.write('\n')

    newf.write('xd = %f' % XCoord[3])
    newf.write('\n')
    newf.write('yd = %f' % YCoord[3])
    newf.write('\n')
    newf.write('zd = %f' % ZCoord[3])
    newf.write('\n')

    newf.write('n1 = %d' % Div1)
    newf.write('\n')
    newf.write('n2 = %d' % Div2)
    newf.write('\n')

    newf.write('sn = %d' % StartN)
    newf.write('\n')
    newf.write('se = %d' % StartE)
    newf.write('\n')

    newf.write("Path = '%s'" % ResultPath)
    newf.write('\n')
    newf.write("Filename = '%s'" % ResultName)
    newf.write('\n')

    newf.write("TypeofEle = '%s'" % EleType)
    newf.write('\n')
    newf.write("SectionNum = %d" % SectionNo)
    newf.write('\n')

    StartCopy = False
    for line in MeshS:
        if 'Input complete' in line:
            StartCopy = True
        if StartCopy:
            newf.write(line)
    newf.close()
    MeshS.close()

    cmd = "python '%s' 1" % FilePath
    p = subprocess.Popen(cmd, shell=True)
    if wait:
        p.communicate()

def CombineItems(FilePath, ResultPath, ResultName, MeshFiles, wait = True):
    global CombineScript
    newf = open(FilePath, 'w')
    CombineS = open(CombineScript, 'r')

    MeshFileList = []
    for obj in MeshFiles:
        MeshFileList.append(ResultPath + '/' + obj)

    newf.write('''inputfiles = %s''' % str(MeshFileList))
    newf.write('\n')
    newf.write('outputfiles = ' + '''"''' + ResultPath + '/' + ResultName + '''"''')
    newf.write('\n')

    StartCopy = False
    for line in CombineS:
        if 'Input complete' in line:
            StartCopy = True
        if StartCopy:
            newf.write(line)
    newf.close()
    CombineS.close()

    cmd = "python '%s' 3" % FilePath
    p = subprocess.Popen(cmd, shell=True)
    if wait:
        p.communicate()


f1 = 'Mesh1.txt'
MeshQuad('/media/chenting/Work/ProgramCode/OpenSeesModellingHelper/GeneratedFiles/MeshTest1.py', '/media/chenting/Work/ProgramCode/OpenSeesModellingHelper/GeneratedFiles', f1, [3, 0, 0, 3], [-2, -2, 2, 2], [0, 0, 0, 0], CurrentNodeNo, CurrentEleNo, 10, 6, 'ShellNL', 4, wait=False)
f2 = 'Mesh2.txt'
MeshQuad('/media/chenting/Work/ProgramCode/OpenSeesModellingHelper/GeneratedFiles/MeshTest2.py', '/media/chenting/Work/ProgramCode/OpenSeesModellingHelper/GeneratedFiles', f2, [-3, 0, 0, -3], [-2, -2, 2, 2], [0, 0, 0, 0], CurrentNodeNo, CurrentEleNo, 10, 6, 'ShellNL', 4)

CombineItems('/media/chenting/Work/ProgramCode/OpenSeesModellingHelper/GeneratedFiles/CombineTest12.py', '/media/chenting/Work/ProgramCode/OpenSeesModellingHelper/GeneratedFiles', 'Combined12.tcl', [f1, f2])

